// pages/certificates/certificates.js
const app = getApp()
var uploadImage = require('../../utils/uploadFile.js');
var util = require('../../utils/util.js');
import { HTTP } from '../../utils/https.js'
let http = new HTTP()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    xinxiang:'', //形象图
    cardfront:'../../image/cardfront.png', //身份证正面
    cardback:'../../image/cardback.png', //身份证反面
    hasrenzheng:'',
    authenticate_message:'',
    type:'', //认证类型,
    identity_children:'', //身份类型
    infor:'',//基本信息
  },
  onLoad:function(options){
    this.setData({
      hasrenzheng: options.hasrenzhen,
      type:options.type,
      identity_children:options.identity_children,
      xinxiang:options.type == 'company' ? 'https://huoxi.oss-cn-beijing.aliyuncs.com/cbb/159495597863334.jpg':'../../image/shezhibg.jpg'
    })
    
    var that = this;
    if (!!options.hasrenzhen){
      http.request({
        url: 'mini/v1/personal/info',
        success: function (res) {
          that.setData({
            infor:res.data,
            xinxiang: that.data.type == 'company' ? res.data.company_license_url:res.data.personal_img,
            cardfront: that.data.type == 'company' ? res.data.company_idcard_front : res.data.idcard_front,
            cardback: that.data.type == 'company' ? res.data.company_idcard_back : res.data.idcard_back,
            authenticate_message: res.data.auth_message
          })
        }
      })
    }
  },
  goindex:function(){
    wx.switchTab({
      url: '../../pages/index/index'
    });
  },
  //选择形象
    choosexinx: function () {
      this.setData({
        xinxiang:this.data.xinxiang
      })

      var name = 'xinxiang';
      var that = this;
      if (this.data.hasrenzheng !== 'renzheng') {
        util.upimg(name, that);
      }
    },
  //选择身份证正面
    cardfront: function () {
      this.setData({
        cardfront:this.data.cardfront
      })

        var name = 'cardfront';
        var that = this;
        util.upimg(name, that);
        console.log(this.data.cardfront)
      },
  //选择身份证背面
   cardback: function () {
    this.setData({
      cardback:this.data.cardback
    })

      var name = 'cardback';
      var that = this;
      if (this.data.hasrenzheng !== 'renzheng') {
        util.upimg(name, that);
      }
    },
    goindexurl:function(e){
     //判断如果是公司需要验证是否填写公司信息开始
        if(this.data.type == "company"){
          const value = e.detail.value
          if(value.companyname == ''){
            wx.showToast({
              title: '填写公司名称！',
              icon: 'none',
              duration: 2000
            });
            return false
          }else if(value.zhizhaonumber == ''){
            wx.showToast({
              title: '填写营业执照号！',
              icon: 'none',
              duration: 2000
            });
            return false
          }else if(value.jbrname == ''){
            wx.showToast({
              title: '填写经办人姓名！',
              icon: 'none',
              duration: 2000
            });
            return false
          }else if(value.idcard == ''){
            wx.showToast({
              title: '填写证件号！',
              icon: 'none',
              duration: 2000
            });
            return false
          }
        }
      //判断如果是公司需要验证是否填写公司信息结束
      if (this.data.xinxiang == "../../image/shezhibg.jpg" || this.data.xinxiang == 'https://huoxi.oss-cn-beijing.aliyuncs.com/cbb/159495597863334.jpg') {
        wx.showToast({
          title: this.data.type == 'company' ? '上传营业执照' : '上传形象图',
          icon: 'none',
          duration: 2000
        });
        return false;
      }

      if (this.data.cardfront == "../../image/cardfront.png") {
        wx.showToast({
          title: '请上传身份证正面！',
          icon: 'none',
          duration: 2000
        });
        return false;
      }

      if (this.data.cardback == "../../image/cardback.png") {
        wx.showToast({
          title: '请上传身份证背面！',
          icon: 'none',
          duration: 2000
        });
        return false;
      }
      
      if(this.data.type == 'company'){
        var that = this;
        const value = e.detail.value
        let data = {
          company_name:value.companyname,
          company_license_no:value.zhizhaonumber,
          company_people:value.jbrname,
          company_idcard:value.idcard,
          company_license_url: this.data.xinxiang,
          company_idcard_front: this.data.cardfront,
          company_idcard_back: this.data.cardback,
          identity_children:this.data.type == 'company' ? 'company' : 'personal'
        };
        http.request({
          url:'mini/v1/personal/company_auth',
          data: data,
          methods: 'post',
          success: function (res) {
            wx.switchTab({
              url:'../../pages/person/person'
            });
          }
        });
        return false
      }

      var that = this;
      let data = {
        personal_img: this.data.xinxiang,
        idcard_front: this.data.cardfront,
        idcard_back: this.data.cardback,
        identity_children:'personal'
      };
      http.request({
        url: 'mini/v1/personal/personal_auth',
        data: data,
        methods: 'post',
        success: function (res) {
          wx.switchTab({
            url:'../../pages/person/person'
          });
        }
      })
    },
    /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return util.share()
  }
})